<template>
  <div>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
      >
        <!-- 文章列表 -->
        <article-item :obj="item" v-for="item in articlesList" :key="item.art_id" @click.native="$router.push(`/article_detail?art_id=${item.art_id}`)"></article-item>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import ArticleItem from '../../components/ArticleItem.vue'
import { getArticlesList } from '@/api'
export default {
  name: 'ArtList',
  props: {
    cid: {
      type: Number
    }
  },
  components: {
    ArticleItem
  },
  data () {
    return {
      params: {
        // 获取文章列表参数
        channel_id: this.cid,
        timestamp: new Date().getTime()
      },
      articlesList: [],
      loading: false, // 加载更多状态
      finished: false,
      refreshing: false // 顶部加载状态
    }
  },
  mounted () {
    this.getArtList()
  },
  methods: {
    // 获取-文章列表
    async getArtList () {
      const res = await getArticlesList(this.params)
      console.log(res)
      this.articlesList = [...this.articlesList, ...res.data.data.results]
      this.params.timestamp = res.data.data.pre_timestamp
      this.loading = false
      this.refreshing = false

      if (res.data.data.pre_timestamp === null) {
        this.finished = true
      }
    },
    // 加载更多事件
    onLoad () {
      // 异步更新数据
      if (this.articlesList.length > 0) {
        this.getArtList()
      }
    },
    // 顶部加载事件
    onRefresh () {
      this.articlesList = []
      this.params.timestamp = new Date().getTime()
      this.getArtList()
    }
  }
}
</script>
